package gui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

import logic.file.IFile;

//TODO zmiana na dziedziczenie po AbstractTableModel
//DefaultModel tylko dla szybszego testowania innych funkcjonalności, jest niewydajny - używa synchronizowanych wektorów
@SuppressWarnings("serial")
public class FilesTableModel extends AbstractTableModel {
	
	public static final int COLUMNS = 5;
	
	public static final int NAME_COLUMN = 0;
	public static final int TYPE_COLUMN = 1;
	public static final int SIZE_COLUMN = 2;
	public static final int DATE_COLUMN = 3;
	public static final int PERMISSIONS_COLUMN = 4;
	
	private String[] colsNames;
	private List<IFile> rows;
	
	public FilesTableModel(List<IFile> rows, String[] colsNames) {
		if (colsNames == null) {
			colsNames = new String[COLUMNS];
		}
		this.colsNames = colsNames;
		if (rows == null) {
			rows = new ArrayList<IFile>();
		}
		this.rows = rows;
	}

	@Override
	public int getColumnCount() {
		return colsNames.length;
	}

	@Override
	public int getRowCount() {
		return rows.size();
	}

	@Override
	public Object getValueAt(int row, int col) {
		IFile file = rows.get(row);
		switch (col) {
			case NAME_COLUMN : return file.getName(); 
			case TYPE_COLUMN : return file.getType(); 
			case SIZE_COLUMN : return file.getSize(); //ew. getFolderSize , + customCellRenderer
			case DATE_COLUMN : return file.getLastModifiedDate();		//DateFormat
			case PERMISSIONS_COLUMN : return file.getFilePermissions(); 
		}
		return null;
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {

		switch (columnIndex) {
			case NAME_COLUMN 		: return String.class;
			case TYPE_COLUMN 		: return String.class;
			case SIZE_COLUMN 		: return Long.class;
			case DATE_COLUMN 		: return Long.class;			//TODO na LONG
			case PERMISSIONS_COLUMN : return String.class;
			default : return null;
		}
	}
	
	@Override
	public String getColumnName(int colIdx) {
		return colsNames[colIdx];
	}
	
	public void addRow(IFile row) {
		rows.add(row);
	}
	
	public void setData(List<IFile> rows) {
		this.rows = rows;
		fireTableDataChanged();
	}

	public IFile getRow(int rowIndex) {
		return rows.get(rowIndex);
	}
	
	//testowo
	public List<IFile> getRows() {
		return rows;
	}

	public void sort(Comparator<IFile> comparator, SortDirection direction) {
		
		if (direction == SortDirection.DESC) {
			comparator = Collections.reverseOrder(comparator);
		}
		
		Collections.sort(rows, comparator);
	}
	
}
